<template>
  <div class="goods-card" @click="goToDetail">
    <!-- 商品图片 -->
    <div class="image-container">
      <el-image
        :src="goods.images?.[0] || defaultImage"
        :alt="goods.title"
        class="goods-image"
        fit="cover"
        lazy
      >
        <template #error>
          <div class="image-error">
            <el-icon><Picture /></el-icon>
            <span>暂无图片</span>
          </div>
        </template>
      </el-image>
      
      <!-- 商品状态标签 -->
      <div class="status-badge" :class="`status-${goods.status}`">
        {{ goods.status }}
      </div>
      
      <!-- 收藏按钮 -->
      <div class="collect-btn" @click.stop="toggleCollect">
        <el-icon :class="{ collected: isCollected }">
          <Star v-if="isCollected" />
          <StarFilled v-else />
        </el-icon>
      </div>
    </div>

    <!-- 商品信息 -->
    <div class="goods-info">
      <h3 class="goods-title">{{ goods.title }}</h3>
      <p class="goods-description">{{ truncateText(goods.description, 60) }}</p>
      
      <!-- 价格信息 -->
      <div class="price-info">
        <span class="current-price">¥{{ formatPrice(goods.price) }}</span>
        <span v-if="goods.originalPrice" class="original-price">
          原价 ¥{{ formatPrice(goods.originalPrice) }}
        </span>
      </div>
      
      <!-- 商品属性 -->
      <div class="goods-meta">
        <el-tag size="small" type="info">{{ goods.category }}</el-tag>
        <el-tag size="small" type="success">{{ goods.condition }}</el-tag>
      </div>
      
      <!-- 卖家信息 -->
      <div class="seller-info">
        <el-avatar :src="goods.sellerAvatar" :size="24">
          {{ goods.sellerName?.charAt(0) }}
        </el-avatar>
        <span class="seller-name">{{ goods.sellerName }}</span>
        <span class="publish-time">{{ formatTime(goods.createdAt) }}</span>
      </div>
      
      <!-- 统计信息 -->
      <div class="stats-info">
        <span class="stat-item">
          <el-icon><View /></el-icon>
          {{ goods.viewCount || 0 }}
        </span>
        <span class="stat-item">
          <el-icon><Star /></el-icon>
          {{ goods.collectCount || 0 }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { collectApi } from '@/api/collect'
import { formatPrice, formatTime, truncateText } from '@/utils/format'
import { ElMessage } from 'element-plus'
import { 
  Picture, 
  Star, 
  StarFilled, 
  View 
} from '@element-plus/icons-vue'

// Props
const props = defineProps({
  goods: {
    type: Object,
    required: true
  }
})

const router = useRouter()
const userStore = useUserStore()

// 默认图片
const defaultImage = 'https://images.pexels.com/photos/4386321/pexels-photo-4386321.jpeg?auto=compress&cs=tinysrgb&w=400'

// 收藏状态
const isCollected = ref(false)

// 检查收藏状态
const checkCollectStatus = async () => {
  if (userStore.isLoggedIn) {
    try {
      const response = await collectApi.checkCollected(props.goods.goodsId)
      isCollected.value = response.data.isCollected
    } catch (error) {
      console.error('检查收藏状态失败:', error)
    }
  }
}

// 切换收藏状态
const toggleCollect = async () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }

  try {
    if (isCollected.value) {
      await collectApi.removeCollect(props.goods.goodsId)
      isCollected.value = false
      ElMessage.success('已取消收藏')
    } else {
      await collectApi.addCollect(props.goods.goodsId)
      isCollected.value = true
      ElMessage.success('收藏成功')
    }
  } catch (error) {
    console.error('收藏操作失败:', error)
    ElMessage.error('操作失败，请重试')
  }
}

// 跳转到商品详情
const goToDetail = () => {
  router.push(`/goods/${props.goods.goodsId}`)
}

// 初始化时检查收藏状态
checkCollectStatus()
</script>

<style scoped>
.goods-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.goods-card:hover {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  transform: translateY(-4px);
}

.image-container {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.goods-image {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.goods-card:hover .goods-image {
  transform: scale(1.05);
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #f5f7fa;
  color: #999;
}

.image-error .el-icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.status-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  backdrop-filter: blur(10px);
}

.status-在售 {
  background-color: rgba(82, 196, 26, 0.9);
  color: white;
}

.status-已售 {
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
}

.status-下架 {
  background-color: rgba(250, 140, 22, 0.9);
  color: white;
}

.collect-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.collect-btn:hover {
  background-color: white;
  transform: scale(1.1);
}

.collect-btn .el-icon {
  font-size: 16px;
  color: #999;
  transition: color 0.3s ease;
}

.collect-btn .el-icon.collected {
  color: #f56c6c;
}

.goods-info {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.goods-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.goods-description {
  font-size: 14px;
  color: #666;
  margin: 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.price-info {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 4px 0;
}

.current-price {
  font-size: 20px;
  font-weight: 700;
  color: #e74c3c;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.goods-meta {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.seller-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.seller-name {
  font-size: 13px;
  color: #666;
  font-weight: 500;
}

.publish-time {
  font-size: 12px;
  color: #999;
  margin-left: auto;
}

.stats-info {
  display: flex;
  gap: 15px;
  margin-top: 8px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #999;
}

.stat-item .el-icon {
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .image-container {
    height: 160px;
  }
  
  .goods-info {
    padding: 12px;
  }
  
  .goods-title {
    font-size: 15px;
  }
  
  .current-price {
    font-size: 18px;
  }
}
</style>